DenoからGoogle Apps Scriptの型定義ファイルを使えるか試す
2021-07-11
型定義ファイルはこちら
https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/master/types/google-apps-script/index.d.ts
どうやって使えばいいかな?
18:34:39 そのまま使うのは無理そう
型定義ファイルのmodule解決の方法がNode用になっている
対策
Deno用に書き換える
/// <reference path="を/// <reference path="./に置換すればいけそう
こんな感じのscriptを組む
1. https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/google-apps-script にあるfileを全てdownloadする
octokit.jsを使ってdownloadする
code:sh
deno run --unstable --allow-net=api.github.com,raw.githubusercontent.com --allow-write=./ --allow-read=./ -r=https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/test.js https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/test.js TOKEN
code:test.ts
import {download} from "./fetch.js";
download();
code:fetch.d.ts
export function download(): Promise<void>;
code:fetch.js
import { Octokit } from "https://esm.sh/octokit@1.1.0?no-check";
import { exists } from "https://deno.land/std@0.100.0/fs/mod.ts";
// 認証
// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: Deno.args0 });
export async function download() {
await expand('types/google-apps-script');
}
async function expand(path) {
const options = {
owner: 'DefinitelyTyped',
repo: 'DefinitelyTyped',
ref: 'master',
};
const {data} = await octokit.rest.repos.getContent({
path,
...options,
});
for (const content of data) {
if (content.type === 'dir') {
if (!(await exists(content.name))) {
await Deno.mkdir(content.name);
}
await Deno.chdir(content.name);
await expand(content.path);
await Deno.chdir('../');
continue;
}
const res = await fetch(content.download_url);
const code = await res.text();
await Deno.writeTextFile(content.name, code);
}
}
2. /// <reference path="を/// <reference path="./に置換する
code:sh
deno run --unstable --allow-write=./ --allow-read=./ -r=https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/convert.ts https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/test2.ts
code:test2.ts
import {convert} from "./convert.ts";
convert();
code:convert.ts
import { expandGlob } from "https://deno.land/std@0.100.0/fs/mod.ts";
export async function convert() {
for await (const file of expandGlob('**/*.ts')) {
const code = await Deno.readTextFile(file.path);
await Deno.writeTextFile(
file.path,
//code.replaceAll(/// <reference path=", /// <reference path="./)
code.replace(/\/\/\/\s*<reference\s*path="(^"+)"\s*\/>/g, '/// <reference path="./$1" />\n// @deno-types="./$1"')
);
}
}
19:59:14 codeは完成した
code:sh
deno run --unstable --allow-net=api.github.com,raw.githubusercontent.com --allow-write=./ --allow-read=./ https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/script.ts TOKEN
code:script.ts
import {download} from "./fetch.js";
import {convert} from "./convert.ts";
await download();
await convert();
生成した型定義ファイルは、Denoで型定義ファイルを読み込#60eacf871280f00000a150a9を使って読み込む
/icons/fail.iconesm.shなどで型定義ファイルをbundleできないか試す
18:36:18 だめでした
https://esm.sh/@types/google-apps-script
エラーが出てしまった
20:53:02 githubに型定義ファイルを置いた
/icons/github.icontakker99/deno-gas-types
https://raw.githubusercontent.com/takker99/deno-gas-types/main/mod.d.ts
LICENSEの書き方わからない……
20:57:46 あれ?GitHubから直接importしようとすると読み込めない?
もしかして<reference path=>はURLを解決してくれないのか?
まじかー……takker.icon
流石に全てのnamed Importを書くのは大変だぞ……
自動置換では対処できない
副作用付きimportでなんとかなるかな?
なんとかならなかった
global変数は副作用付きimportでなんとかなるけど、それ以外の型はnamed importしないと無理
21:24:33 @deno-typesを使ってもだめだった……
毎回毎回localに型定義をdownloadするか、Denoから読み込めるような型定義ファイルを自作するしかないなあ……
2021-07-13 19:20:01 Deno用の型定義ファイルを作っている事例を見つけた
https://github.com/Soremwar/deno_types/blob/master/react/v16.13.1/react.d.ts
/icons/github.iconSoremwar/deno_types
もうメンテはされていない模様
2021-07-25
14:04:20 <reference types=>を使えばいけるっぽい?
from https://deno.land/manual@v1.12.1/typescript/types#using-the-triple-slash-reference-directive
やってみる
code:bash
deno run --unstable --allow-net=api.github.com,raw.githubusercontent.com --allow-write=./ --allow-read=./ https://scrapbox.io/api/code/takker/DenoからGoogle_Apps_Scriptの型定義ファイルを使えるか試す/script2.ts TOKEN
code:script2.ts
import {download} from "./fetch.js";
import {convert} from "./convert2.ts";
await download();
await convert();
code:convert2.ts
import { expandGlob } from "https://deno.land/std@0.100.0/fs/mod.ts";
export async function convert() {
for await (const file of expandGlob('**/*.ts')) {
const code = await Deno.readTextFile(file.path);
await Deno.writeTextFile(
file.path,
//code.replaceAll(/// <reference path=", /// <reference path="./)
code.replace(/\/\/\/\s*<reference\s*path="(^"+)"\s*\/>/g, '/// <reference types="./$1" />')
);
}
}
#Google_Apps_Script
#2021-08-10 09:35:08
#2021-07-25
#2021-07-13 19:21:31
#2021-07-11 18:24:20